package com.ad.pet.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ad.pet.Logger;
import com.ad.pet.R;
import com.ad.pet.app.RxDisposable;

import java.util.Timer;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Action1;

/**
 * Created by SEELE on 2019/7/12.
 */

public class TimeLabelZTextview extends LinearLayout {
    private Context mContext;
    private View mView;
    private TextView mTv1;
    private TextView mTv2;
    private TextView mTvHour;
    private TextView mTvMinus;
    private TextView mTvSecond;

    boolean isFlag = true;
    private int downTIme = 99;
    /**
     * 一小时的秒数
     */
    private static final int HOUR_SECOND = 60 * 60;

    /**
     * 一分钟的秒数
     */
    private static final int MINUTE_SECOND = 60;
    private long mTime = 0;
    private Timer mTimer;

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            // 要做的事情
            super.handleMessage(msg);

        }
    };
    private Runnable mRunnable;
    private TextView mTvDay;
    private TextView mTv0;
    private RxDisposable mRxDisposable;


    public TimeLabelZTextview(Context context) {
        super(context);
        initView(context);
    }

    public TimeLabelZTextview(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mRxDisposable = new RxDisposable();
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.item_time_label_layout, this, true);
        mTvDay = mView.findViewById(R.id.tv_day);
        mTvHour = mView.findViewById(R.id.tv_hour);
        mTvMinus = mView.findViewById(R.id.tv_minus);
        mTvSecond = mView.findViewById(R.id.tv_second);
        mTv1 = mView.findViewById(R.id.tv_1);
        mTv2 = mView.findViewById(R.id.tv_2);
        mTv0 = mView.findViewById(R.id.tv_0);

        mRunnable = new Runnable() {
            @Override
            public void run() {

                downTIme--;
                String a = "";


                if (downTIme == 0) {
                    downTIme = 99;
                }
                if (downTIme < 10) {
                    a = "0" + downTIme;
                } else {
                    a = String.valueOf(downTIme);
                }

                mTvSecond.setText(a);
                handler.postDelayed(this, 10);
            }
        };
        handler.postDelayed(mRunnable, 10);


    }

    private void setDownTime(long second) {
        long hours = second / HOUR_SECOND;
        if (hours > 0) {

            second -= hours * HOUR_SECOND;
        }

        long minutes = second / MINUTE_SECOND;
        if (minutes > 0) {

            second -= minutes * MINUTE_SECOND;
        }


        String time = ((hours >= 10 ? (hours + "")
                : ("0" + hours)) + ":" + ((minutes >= 10 ? (minutes + "") : ("0" + minutes))) + ":"
                + ((second >= 10 ? (second + "") : ("0" + second))));


        Logger.e("time-->" + time);

        String[] strings = time.split(":");

        mTvDay.setText(strings[0]);
        mTvHour.setText(strings[1]);
        mTvMinus.setText(strings[2]);


    }

    public void setDownTimeStart(long time, final onDownTimeLinstener onDownTimeLinstener) {
        mTime = time;
        if (isFlag) {
            mRxDisposable.add(Observable.interval(1, TimeUnit.SECONDS)
                    .subscribe(new Action1<Long>() {
                        @Override
                        public void call(Long aLong) {
                            ((Activity) mContext).runOnUiThread(new Runnable() {   // UI thread
                                @Override
                                public void run() {
                                    mTime--;
                                    setDownTime(mTime);
                                    if (mTime <= 0) {
                                        onDownTimeLinstener.onTimeEnd();
                                        mTimer.cancel();
                                        handler.removeCallbacks(mRunnable);
                                    }
                                }
                            });
                        }
                    }));
        }

///        if (isFlag) {
//            final TimerTask task = new TimerTask() {
//                @Override
//                public void run() {
//                    ((Activity) mContext).runOnUiThread(new Runnable() {   // UI thread
//                        @Override
//                        public void run() {
//                            mTime--;
//                            setDownTime(mTime);
//                            if (mTime <= 0) {
//                                onDownTimeLinstener.onTimeEnd();
//                                mTimer.cancel();
//                                handler.removeCallbacks(mRunnable);
//                            }
//                        }
//                    });
//                }
//            };

//            mTimer = new Timer();
//            mTimer.schedule(task, 1, time);
//        }
        isFlag = false;
    }


    public void setTimeColor(int color) {
        mTvDay.setTextColor(mContext.getResources().getColor(color));
        mTvHour.setTextColor(mContext.getResources().getColor(color));
        mTvMinus.setTextColor(mContext.getResources().getColor(color));
        mTvSecond.setTextColor(mContext.getResources().getColor(color));
    }

    public void setTimeBgColor(int color) {

        mTvHour.setBackgroundColor(mContext.getResources().getColor(color));
        mTvMinus.setBackgroundColor(mContext.getResources().getColor(color));
        mTvSecond.setBackgroundColor(mContext.getResources().getColor(color));
        mTvDay.setBackgroundColor(mContext.getResources().getColor(color));
    }

    public void setHaoColor(int color) {
        mTv1.setTextColor(mContext.getResources().getColor(color));
        mTv2.setTextColor(mContext.getResources().getColor(color));
        mTv0.setTextColor(mContext.getResources().getColor(color));

    }


    public interface onDownTimeLinstener {
        void onTimeEnd();

    }


    public long getDownTIme() {
        return mTime;
    }


    private static String unitFormat(long i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + i;
        else
            retStr = "" + i;
        return retStr;

    }


    private static String secToTime(long time) {
        String timeStr = null;
        long hour = 0;
        long minute = 0;
        long second = 0;
        if (time <= 0)
            return "00:00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time % 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
//        long timeStamp = time*1000;  //获取当前时间戳
//        timeStamp = 1392515067621L+timeStamp;
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
//        String sd = simpleDateFormat.format(new Date(timeStamp));

        return timeStr;
    }


}
